
//
//  NewsScrollView.m
//  HenanOA
//
//  Created by 党阳阳 on 14-5-21.
//  Copyright (c) 2014年 broadwave.com. All rights reserved.
//

#import "NewsScrollView.h"
#import "ReactiveCocoa/ReactiveCocoa.h"
#import "NewsScrollViewCell.h"
#import "NSString+HTML.h"
#import "XHHotPicViewModel.h"


@interface NewsScrollView()
@property (nonatomic,strong) UIScrollView *scrollView;
@property (nonatomic,strong) UIPageControl *pageControl;
@property (assign) BOOL didEndAnimate;
@property (assign) float previousTouchPoint;
@property (nonatomic ,strong) XHHotPicViewModel *viewModel;
@property (nonatomic, strong) NSArray* allArr;


@end

@implementation NewsScrollView
@synthesize allArr = _allArr;
- (id)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        // Initialization code
        
        _allArr = [NSArray array];
        _scrollView = [[UIScrollView alloc]initWithFrame:CGRectMake(0, 0, self.frame.size.width, self.frame.size.height)];
        _scrollView.showsVerticalScrollIndicator = NO;
        _scrollView.showsHorizontalScrollIndicator = NO;
        
        _scrollView.pagingEnabled = YES;
        _scrollView.delegate = self;
        _scrollView.contentSize = CGSizeMake(self.frame.size.width*3, self.frame.size.height);
        _scrollView.autoresizesSubviews = YES;
        for (int i=0; i<3; i++) {
            
            
            UIImageView *view = [[UIImageView alloc]initWithFrame:CGRectMake(i*_scrollView.frame.size.width, 0, _scrollView.frame.size.width, _scrollView.frame.size.height)];
            view.image = [UIImage imageNamed:@"base_detail_default_pic.png"];
            [_scrollView addSubview:view];
            
            
            
            
          
        }
        
        _pageControl = [[UIPageControl alloc]initWithFrame:CGRectMake(260, self.frame.size.height-20, 60, 20)];
        _pageControl.numberOfPages = 3;
        _pageControl.currentPage = 0;
        _pageControl.userInteractionEnabled = NO;
        _pageControl.currentPageIndicatorTintColor = [UIColor whiteColor];
        
        [self addSubview:_scrollView];
        [self addSubview:_pageControl];

        self.backgroundColor = [UIColor clearColor];
        
        _viewModel = [[XHHotPicViewModel alloc] init];
         NSUserDefaults* deaf = [NSUserDefaults standardUserDefaults];
        _allArr = [deaf objectForKey:@"新闻热点"];
        [self fillImage:_allArr];
        @weakify(self);
        
        [[_viewModel pullImageNews] subscribeNext:^(id x) {
            @strongify(self);
//            NSLog(@"%@",x);
            _allArr = x;
            NSUserDefaults* deaf = [NSUserDefaults standardUserDefaults];
            [deaf removeObjectForKey:@"新闻热点"];
            [deaf setObject:x forKey:@"新闻热点"];
            [deaf synchronize];
            [self performSelectorOnMainThread:@selector(fillImage:) withObject:x waitUntilDone:YES];
        }];
    }
    return self;
}


- (void)fillImage:(NSArray *)arrayOfImages
{
    
    for (UIView *view in [_scrollView subviews]) {
        [view removeFromSuperview];
    }
    
    NSInteger count = [arrayOfImages count];
    _scrollView.contentSize = CGSizeMake(self.frame.size.width*count, self.frame.size.height);
    _scrollView.autoresizesSubviews = YES;
    
    for (int i=0; i<count; i++) {
        
        CGRect rect = CGRectMake(i*self.frame.size.width, 0, self.frame.size.width, self.frame.size.height);
       
        
        NSString *imageString = [NSString stringWithFormat:@"%@%@",self.viewModel.urlString.ipAndPort,[[arrayOfImages objectAtIndex:i] objectForKey:@"image"]];
       
        NSString *titleString = [[[arrayOfImages objectAtIndex:i] objectForKey:@"title"]stringByReplacingOccurrencesOfString:@"&mdash;" withString:@"--"];
        
        titleString = [titleString removeHTMLCode];
        
        NSString* dfffe = [NSString stringWithFormat:@"%@",[[arrayOfImages objectAtIndex:i] objectForKey:@"id"]];
        NewsScrollViewCell *cell = [[NewsScrollViewCell alloc]initWithFrame:rect imageString:imageString titleString:titleString withnewf:dfffe];
        
        
        cell.tag= [[[arrayOfImages objectAtIndex:i] objectForKey:@"id"] intValue]*10+i;
        NSLog(@"newid = %@ cell.tag = %d  image string = %@",[[arrayOfImages objectAtIndex:i] objectForKey:@"id"],cell.tag,imageString);
        
        
        UITapGestureRecognizer *tapGesture = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(tap:)];
        cell.userInteractionEnabled = YES;
        [cell addGestureRecognizer:tapGesture];
        
        
        [_scrollView addSubview:cell];
    }
    
    _pageControl.numberOfPages = arrayOfImages.count;
    
    
    
}

#pragma -mark UIScrollViewDelegate

- (void)scrollViewDidScroll:(UIScrollView *)scrollView
{
    if ([scrollView isEqual:_scrollView]) {
        
        NSInteger page = fabs(scrollView.contentOffset.x/scrollView.frame.size.width);
        _pageControl.currentPage = page;
   
    }
}


- (void)scrollViewWillBeginDecelerating:(UIScrollView *)scrollView
{
    
}


- (void)tap:(id)sender
{
    UITapGestureRecognizer *tap = (UITapGestureRecognizer *)sender;

    

    NSString *newId = [NSString stringWithFormat:@"%ld",(long)tap.view.tag/10];
    int i = tap.view.tag%10;
    NSLog(@"i =%d",i);
    NSString *imageString = [NSString stringWithFormat:@"%@%@",self.viewModel.urlString.ipAndPort,[[_allArr objectAtIndex:i] objectForKey:@"image"]];
    NSLog(@"imagestring = %@",imageString);
    [_delegate selectNewsScrollViewCellForId:newId withImageString:imageString];
}
/*
// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect
{
    // Drawing code
}
*/

@end
